<template>
  <a-modal title="添加商品" :visible="visible" @ok="add" @cancel="visible = false" :width="1300">
    <div class="searchBox">
      <div class="select">
        <span>商品名称：</span>
        <a-input placeholder="请输入商品名称" v-model="name" allowClear>
          <a-icon slot="suffix" type="search" />
        </a-input>
      </div>

      <a-button type="primary" @click="refresh()">查询</a-button>
    </div>

    <a-table
      ref="tableList"
      :columns="columns"
      :data-source="dataSource"
      :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
      :row-key="record => record.id"
      :pagination="{
        total: this.totalCount,
        showSizeChanger: true,
        onChange: this.tablePageChange,
        onShowSizeChange: this.paginationSizeChange
      }"
      :loading="loading"
      :customRow="
        (record, index) => {
          return {
            on: {
              mouseenter: e => {
                this.currentRowId = record.id
              }
            }
          }
        }
      "
      :scroll="{ y: 300 }"
    >
      <!-- 产品名称 -->
      <template slot="goods_name" slot-scope="text, goods">
        <div class="goods-info">
          <img class="goods-pic" :src="goods.cover_pic" alt="" />
          <div class="info-box">
            <a-tooltip placement="top">
              <template slot="title">
                <span class="goods-name">{{ goods.name }} </span>
              </template>
              <div style="font-weight: bold; line-height: 25px; width: 100%;" class="of_el">
                {{ goods.name }}
              </div>
            </a-tooltip>
            <div style="color: rgb(140, 140, 140); width: 100%;" class="of_el">
              {{ goods.cat ? goods.cat.map(item => item.name).join() : '暂无分类' }}
            </div>
          </div>
        </div>
      </template>

      <!-- 产品类型 -->
      <template slot="product_type" slot-scope="text">
        <span class="goods-type" v-if="text == 0">普通商品</span>
        <span class="goods-type" v-if="text == 1">酒店预约</span>
        <span class="goods-type" v-if="text == 2">服务预约</span>
        <span class="goods-type" v-if="text == 3">虚拟商品</span>
        <span class="goods-type" v-if="text == 4">积分商品</span>
      </template>

      <!-- 售价 -->
      <template slot="price" slot-scope="text, goods">
        <div class="goods-price-row">
          <span>{{ text }}</span>
        </div>
      </template>

      <!-- 库存 -->
      <template slot="goods_num" slot-scope="text, goods">
        <div class="goods-rest-row">
          <span>{{ text }}</span>
        </div>
      </template>

      <!-- 虚拟销量 -->
      <template slot="virtual_sales" slot-scope="text, goods">
        <div class="goods-virtual-row">
          <span>{{ text }}</span>
        </div>
      </template>
    </a-table>
  </a-modal>
</template>

<script>
import { videoGoodsStoreGoodsList } from '@/api/videoClassify'

const columns = [
  {
    title: '商品ID',
    dataIndex: 'id',
    key: 'id',
    width: 80
  },
  {
    title: '产品名称',
    dataIndex: 'name',
    key: 'name',
    scopedSlots: { customRender: 'goods_name' }
  },
  {
    title: '产品类型',
    dataIndex: 'product_type',
    key: 'product_type',
    scopedSlots: { customRender: 'product_type' },
    width: 150
  },
  {
    title: '售价',
    dataIndex: 'price',
    key: 'price',
    defaultSortOrder: '',
    sorter: (a, b) => a.price - b.price,
    scopedSlots: { customRender: 'price' },
    width: 150
  },
  {
    title: '库存',
    dataIndex: 'goods_num',
    key: 'goods_num',
    defaultSortOrder: '',
    sorter: (a, b) => a.goods_num - b.goods_num,
    scopedSlots: { customRender: 'goods_num' },
    width: 150
  },
  {
    title: '虚拟销量',
    dataIndex: 'virtual_sales',
    key: 'virtual_sales',
    defaultSortOrder: '',
    sorter: (a, b) => a.virtual_sales - b.virtual_sales,
    scopedSlots: { customRender: 'virtual_sales' },
    width: 150
  },
  {
    title: '排序',
    dataIndex: 'sort',
    key: 'sort',
    width: 150
  }
]

export default {
  data() {
    return {
      columns,
      selectedRowKeys: [],
      visible: false,
      name: undefined,
      dataSource: [],
      totalCount: 0,
      pageSize: 10,
      pageNo: 1,
      loading: false
    }
  },

  mounted() {
    this.refresh()
  },

  methods: {
    // 获取商品列表数据
    getGoodsData() {
      let { name, pageNo, pageSize } = this
      this.loading = true
      this.selectedRowKeys = []

      return videoGoodsStoreGoodsList({
        name,
        pageNo,
        pageSize
      }).then(res => {
        this.loading = false
        if (!res.code) {
          this.dataSource = res.data.data
          this.totalCount = res.data.totalCount
        }
      })
    },

    // 分页请求
    tablePageChange(e) {
      this.pageNo = e
      this.getGoodsData()
    },

    // 分页器页数修改回调
    paginationSizeChange(current, size) {
      this.pageNo = current
      this.pageSize = size
      this.getGoodsData()
    },

    // 刷新数据
    refresh() {
      this.pageNo = 1
      this.getGoodsData()
    },

    // 表格选中
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    // 新增
    add() {
      let { selectedRowKeys } = this
      if (!selectedRowKeys.length) {
        this.$message.error('请至少选择一条数据~')
        return
      }

      this.$emit('add', selectedRowKeys)
    }
  }
}
</script>
<style lang="less" scoped>
.slotTable {
  /deep/.ant-table-row-cell-break-word {
    padding: 2px 8px !important;
  }
  /deep/.ant-table-body {
    min-height: 0px;
  }
}

/deep/.ant-table-body::-webkit-scrollbar {
  display: none; /*ChromeSafari*/
}

.of_el {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.table_operator_icon {
  font-size: 18px;
  padding: 0 5px;
  color: rgb(182, 182, 182);
  cursor: pointer;
  &:hover {
    color: rgb(24, 144, 255);
  }
}

.goods-info {
  display: flex;
  align-items: flex-start;
  position: relative;

  .goods-pic {
    width: 48px;
    height: 48px;
    border-radius: 4px;
    display: block;
  }
  .info-box {
    margin-left: 24px;
    margin-right: 5px;
    flex: 1;
    overflow: hidden;

    .goods-name {
      width: 100%;
      font-size: 14px;
      line-height: 22px;
      color: rgba(0, 0, 0, 0.65);
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }
  .item-edit-box {
    width: 15px;
    height: 100%;
    position: absolute;
    right: -12px;
    top: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}
.goods-price-row {
  position: relative;
  display: inline-block;
  .row-price-edit {
    position: absolute;
    right: -27px;
    top: 0;
    width: 15px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.goods-rest-row {
  position: relative;
  display: inline-block;
  .row-rest-edit {
    position: absolute;
    right: -27px;
    top: 0;
    width: 15px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.goods-virtual-row {
  position: relative;
  display: inline-block;
  .row-virtual-edit {
    position: absolute;
    right: -27px;
    top: 0;
    width: 15px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.goods-type {
  min-width: 60px;
  display: inline-block;
}

.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}
</style>
